<?php
    @session_start();
    require_once 'shared-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';
    
    if(!IsValidSession())
    {
        header('Location: login.php?page=manage-courses');
        exit();
    }
    else
    {
    	RefreshSession();
    }
	
	$user = GetCurrentUserAccessLevel();
	
    //Check user access
    if($user == $TEACHER || $user == $STUDENT || $user == $REGISTRAR)
    {
        header('Location: login.php?page=manage-courses&error=Insufficient privileges to access to the manage courses page&logout=1');
        exit();
    }

    masterpage("Manage Courses");
	
	echo "<a href='add-courses.php'>Add New Course</a><br/><br/>";

    $link = connect_db();
    $query = "SELECT `CourseID`, `Name`, `Description`, `Credits`, `SpecialFee`, `CourseCode` FROM `Course`";
    $courses = mysql_query($query,$link);
    if(!$courses)
    {
        echo "Oops...something went wrong. Please contact support.";
        exit();
    }
	
    echo "<table class='DataList'>";
    echo "  <thead>";
    echo "    <tr><th class='header'> </th><th class='header'>Name</th><th class='header'>Course Code</th><th class='header'>Description</th><th class='header'>Credits</th><th class='header'>Special Fee</th></tr>";
    echo "  </thead>";
    echo "  <tbody>";

	$rowID = 0;
    while($tblrow = mysql_fetch_array($courses,MYSQL_BOTH))
    {
	  if ($rowID == 0)
	  {
		$rowClass = "rows";
		$rowID++;
	  }
	  else
	  {
		$rowClass = "altrows";
		$rowID--;
	  }
	  echo "<form action='edit-courses.php' method='POST'>";
	  echo "<input type='hidden' name='id' value='$tblrow[CourseID]'/>";
      echo "<tr class='$rowClass'><td><input type='submit' value='Edit'/>";
	  echo "</td><td>".$tblrow['Name']."</td><td>".$tblrow['CourseCode']."</td><td width=\"700\">".$tblrow['Description']."</td><td>".$tblrow['Credits']."</td><td>".$tblrow['SpecialFee']."</td></tr>";
	  echo "</form>";
    }

    echo '  </tbody>';
    echo '</table>';
    
    endmasterpage();
?>

